import 'package:base_flutter/base_flutter.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/model/iot/iot_model.dart';

import '../../chart/char_widget.dart';
import '../../chart/chart_data.dart';
import '../../net/response/iot/mattress_detail_data.dart';
import '../../res/colors.dart';

class MattressDetailViewModel extends BaseListViewModel<MattressDetailData> {
  final String iotCode;

  MattressDetailViewModel(BuildContext context, {required this.iotCode})
      : super(context);

  var model = IotModel();
  var date = "";
  var dateStr = "";
  static const List<String> format = <String>['yyyy', '-', 'mm', '-', 'dd'];
  var today = DateTime.now().toDateStr(format);
  MattressRecordData? detail;

  @override
  Future<List<MattressDetailData>> requestListData() async {
    detail = await model.getMattressDetail(date: date, iotCode: iotCode);
    return detail?.recordList??[];
  }

  @override
  void onCreated() {
    date = today;
    dateStr = date==today?'今天':date;
    super.onCreated();
    requestRefresh(showAni: false);
  }

  Widget topContent() {
    return Column(
      children: [
        Container(
          height: 20,
        ),
        CommonText('$dateStr', textColor: Colors.white, textSize: 24).onTap(() {
          var dateData = DateTime.parse(date);
          DatePicker.showDatePicker(context,currentTime: dateData,onConfirm: (time){
            date = time?.toDateStr(format)??"";
            notifyListeners();
            requestRefresh();
          });
        }),
        Container(
          height: 20,
        ),
        CommonText('${detail?.leaveBedNum??0}',
            textColor: Colors.white, textSize: 60),
        Container(height: 10,),
        CommonText('离床次数',textColor: Colors.white,textSize: 14),
        Container(
          height: 20,
        ),
      ],
    ).addToContainer(width: double.infinity, color: Colours.purpleColor);
  }


  Widget heartChart(){
    var heartList = listItems.map((e) => ChartData(name:e.time?.substring(10)??"",value: e.heart??0)).toList();
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        CommonText('心率曲线').addToContainer(margin: EdgeInsets.symmetric(horizontal: 16,vertical: 10)),
        ChartWidget.lineChart(heartList,showXAis: false,min: 0,max: 200).addToContainer(height: 200)
      ],
    );
  }

  Widget breathChart(){
    var breathList = listItems.map((e) => ChartData(name:e.time?.substring(10)??"",value: e.breath??0)).toList();
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        CommonText('呼吸曲线').addToContainer(margin: EdgeInsets.symmetric(horizontal: 16,vertical: 10)),
        ChartWidget.lineChart(breathList,showXAis: false,min: 0,max: 30).addToContainer(height: 200)
      ],
    );
  }

  @override
  void showEmptyState({String msg = "暂无数据"}) {
    // super.showEmptyState(msg);
    showContent();
  }
}
